<?php

namespace App\Http\Controllers\Admin\qyf;
use App\Common\Responses;
use App\Http\Controllers\Controller;
use App\Models\House;
use App\Utils\Service\ES;
use Illuminate\Http\Request;

class HouseResources extends Controller
{
    //小区列表添加
    function addRegion(Request $request){
        $param = $this->getParam($request);
        $data= House::addHouse($param);
       return Responses::success($data);
    }
    //查询小区列表
    function region(Request $request){
        // 分页查询二手房小区列表
       $data= House::lookHouse(5);
       return Responses::success($data,20000);
    }
    //修改小区
    function updateRegion(Request $request){
        $param = $this->getParam($request);
        $id = (integer) $request->get('id');
        $data= House::updateBroker($id,$param);
        return $data==1?Responses::success('',20000,'修改成功'):
            Responses::error(['Error'],'403','修改失败');
    }
    //删除小区内容
    function deleteRegion(Request $request){
       $id= $request->get('id');
      $data= House::deleteBroker($id);
        return $data==1?Responses::success('',20000,'删除成功'):
            Responses::error(['Error'],'403','删除失败');

    }
function search(Request $request){
  $location=(string) $request->get('location');
  $estate_id=  (int) $request->get('estate_id');
  $region=  (string) $request->get('region');
  $floor=  (int) $request->get('floor');
  $total_floor=  (int) $request->get('total_floor');
  $area=  (double) $request->get('area');
  $price=  (double) $request->get('price');
  $title=  (string) $request->get('title');
  $person= (string) $request->get('person');
   //连接ES
    $client= ES::getClient();
    //判断索引
    $exists = $client->indices()->exists(['index' => 'House_index']);
    //中文分词
    $params = [
    'index' => 'House_index',
    'body' => [
        'settings' => [
            'analysis' => [
                'analyzer' => [
                    'ik_max_word' => [
                        'type' => 'custom',
                        'tokenizer' => 'ik_max_word'
                    ]
                ]
            ]
        ],
        'mappings' => [
            'properties' => [
                'title' => [
                    'type' => 'text',
                    'analyzer' => 'ik_max_word'
                ],
                'content' => [
                    'type' => 'text',
                    'analyzer' => 'ik_max_word'
                ]
            ]
        ]
    ]
];
    if ($exists) return $this->esFind();
        //判断索引不存在则创建索引
    $client->indices()->create($params);
    //初始化索引数据
    $data=House::allHouse();
    // 索引数据
    $params = [
    'index' => 'House_index',
    'body' => $data
    ];
    return Responses::success($client->index($params));
    }

    function esFind(){

    }
    /**
     * @param Request $request
     * @return array
     */
    private function getParam(Request $request): array
    {
        $location = (string)$request->post('location');
        $region = (string)$request->post('region');
        $floor = (integer)$request->post('floor');
        $total_floor = (integer)$request->post('total_floor');
        $area = (float)$request->post('area');
        $price = (float)$request->post('price');
        $title = (float)$request->post('title');
        $person = (float)$request->post('person');
        $param = [
            'location' => $location,
            'region' => $region,
            'floor' => $floor,
            'total_floor' => $total_floor,
            'area' => $area,
            'price' => $price,
            'title' => $title,
            'person' => $person
        ];
        return $param;
    }
}
